#include <iostream>
#include <cstring>
using namespace std;
const int N = 1011;
int dp[N];
int w[N];
int c[N];

int main(){
    int t = 0;
    int M = 0;
    cin >> t>>M;
    for (int i = 1; i <= M; i++) {
        cin >> c[i]>>w[i];
    }
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <=M; i++) {
        for (int j = t; j >=c[i]; j--) {
    dp[j] = max(dp[j-c[i]]+w[i], dp[j]);
        }
    }
    cout << dp[t];
    return 0;
}